# coding = utf-8
'''
火车票核心查询实现
'''
from util.util import *

class Ticket(object):

    def __init__(self,from_station_name,to_station_name,start_date,**kwargs):
        self.__from_station_name = from_station_name
        self.__to_station_name = to_station_name
        self.__start_date = start_date
        if 'options' in kwargs.keys():
            self.__options = kwargs.get('options')
    
    def __check_arguments(self):
        '''
        检查传入参数(私有方法)
        '''
        check_station_name(self.__from_station_name)
        check_station_name(self.__to_station_name)
        check_start_date(self.__start_date)
    
    def query_ticket(self):
        '''
        查询火车票
        '''
        # 校验数据
        self.__check_arguments()
        # 获取火车票信息
        raw_trains = request_kyfw_url(get_station_code(self.__from_station_name),
                    get_station_code(self.__to_station_name),self.__start_date)

        rs = list()
        for raw_train in raw_trains:
            data_list = raw_train.split('|')
            train_no = data_list[3]
            inital = train_no[0].lower()
            if not self.__options or inital in self.__options:
                td =[
                    train_no,
                    set_color(get_station_name(data_list[6]),get_station_name(data_list[7])),
                    set_color(data_list[8],data_list[9]),
                    format_duration(data_list[10]),
                    data_list[31] or '--',
                    data_list[30] or '--',
                    data_list[23] or '--',
                    data_list[28] or '--',
                    data_list[29] or '--',
                    data_list[26] or '--']
                #添加到rs
                rs.append(td)
        #打印
        pretty_print(rs)





    
        
    





    
